package com.hcbd.tdwt.ui.haige.common.adapter;

import android.app.Activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.haige.common.vo.SecurityContentVO;

import java.util.List;

public class SecurityContentAdapter extends RecyclerView.Adapter<SecurityContentAdapter.MyViewHolder> {

    protected final Context mContext;
    private List<SecurityContentVO> contentList;
    private final LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    private int index=0;
    public SecurityContentAdapter(Context context, List<SecurityContentVO> list) {
        mContext = context;
        mInflater = ((Activity) context).getLayoutInflater();
        contentList = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.security_content_item_haige, viewGroup, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        SecurityContentVO securityContentVO=contentList.get(i);
        if(securityContentVO.id==1||securityContentVO.id==2){
            myViewHolder.delImg.setVisibility(View.GONE);
        }
        myViewHolder.textName.setText(securityContentVO.content);
        myViewHolder.delImg.setOnClickListener(v -> onItemClickListener.delete(securityContentVO));
        if(securityContentVO.isSelect){
            myViewHolder.textName.setTextColor(ContextCompat.getColor(mContext,R.color.blue_400));
        }else{
            myViewHolder.textName.setTextColor(ContextCompat.getColor(mContext,R.color.black));
        }
        myViewHolder.textName.setOnClickListener(v -> onItemClickListener.selectIndex(securityContentVO));
    }

    @Override
    public int getItemCount() {
        return contentList.size();
    }

    public void notifyDataSetChanged(List<SecurityContentVO> list) {
        contentList = list;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textName;
        ImageView delImg;
        MyViewHolder(View itemView) {
            super(itemView);
            textName=itemView.findViewById(R.id.textName);
            delImg=itemView.findViewById(R.id.delImg);
        }
    }

    public interface OnItemClickListener {
        void selectIndex(SecurityContentVO securityContentVO);
        void delete(SecurityContentVO securityContentVO);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
